package com.steel.yfmall.cloud.assistant.impl;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.api.stock.OrderLogisticsService;
import com.steel.yfmall.cloud.assistant.bean.AssistantConstants;
import com.steel.yfmall.cloud.assistant.bean.param.ParaCreateOrderDepotShipBean;
import com.steel.yfmall.cloud.assistant.dto.para.oms.ParaMasterOrderInfo;
import com.steel.yfmall.cloud.assistant.oms.MasterOrderInfoService;
import com.steel.yfmall.cloud.assistant.oms.MasterOrderOmsAPIService;
import com.steel.yfmall.cloud.assistant.oms.mapper.MasterOrderInfoExtendMapper;
import com.steel.yfmall.cloud.assistant.oms.mapper.MasterOrderInfoMapper;
import com.steel.yfmall.cloud.assistant.oms.mapper.OrderDistributeMapper;
import com.steel.yfmall.cloud.assistant.po.oms.*;
import com.steel.yfmall.cloud.components.mq.bean.RequestSendMQBean;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.SOAStateBean;
import com.steel.yfmall.cloud.oms.api.bean.request.RequestPayBackBean;
import com.steel.yfmall.cloud.oms.manager.api.bean.para.RequestOrderQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.0
 * @ClassName: ZgOrderServiceImpl
 * @author: TQ
 * @Description: DOTO
 * @Date Create in 14:59 2020/11/19
 **/
@Service
@Slf4j
public class OrderLogisticsServiceImpl implements OrderLogisticsService {

    @Resource
    private MasterOrderInfoService masterOrderInfoService;

    @Resource
    private OrderDistributeMapper orderDistributeMapper;

    @Resource
    private MqSendService mqSendService;

    @Resource
    private MasterOrderInfoExtendMapper masterOrderInfoExtendMapper;

    @Resource
    private MasterOrderInfoMapper masterOrderInfoMapper;

    @Override
    public APIMsgBean<Boolean> createOrderDepotShip(ParaCreateOrderDepotShipBean paraCreateOrderDepotShipBean) {
        log.info("createOrderDepotShip接口接收参数"+JSON.toJSONString(paraCreateOrderDepotShipBean));
        APIMsgBean<Boolean> resultApi = new APIMsgBean<>();
        resultApi.setData(false);
        try {
            if (paraCreateOrderDepotShipBean == null || paraCreateOrderDepotShipBean.getSpotStatus() == null){
                resultApi.getSoaStateBean().setMessage("参数不能为空！");
                return resultApi;
            }
            //出库
            if (paraCreateOrderDepotShipBean.getSpotStatus() == 3){
                if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getStatusTime()) || paraCreateOrderDepotShipBean.getShipWgt() == null) {
                    resultApi.getSoaStateBean().setMessage("出库时间与出库实际重量不能为空！");
                    return resultApi;
                }
            }

            //调用oms接口 根据外部合同号验证订单是否存在
            MasterOrderInfoExample example = new MasterOrderInfoExample();
            example.createCriteria().andMasterOrderSnEqualTo(paraCreateOrderDepotShipBean.getMasterOrderSn());
            MasterOrderInfo masterOrderInfo = masterOrderInfoMapper.selectOneByExampleSelective(example);

            //先查询交货单表
            OrderDistributeExample orderDistributeExample = new OrderDistributeExample();
            orderDistributeExample.createCriteria().andMasterOrderSnEqualTo(masterOrderInfo.getMasterOrderSn());
            List<OrderDistribute> orderDistributes = orderDistributeMapper.selectByExample(orderDistributeExample);
            if (orderDistributes == null || orderDistributes.size() == 0) {
                resultApi.getSoaStateBean().setMessage("订单未确认，无法修改状态");
                return  resultApi;
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date parse = simpleDateFormat.parse(paraCreateOrderDepotShipBean.getStatusTime());

            //更新表字段状态
            if (paraCreateOrderDepotShipBean.getSpotStatus() == 3) {
                MasterOrderInfo record = new MasterOrderInfo();
                record.setSpotStatus(3);
                record.setClearGoodsWeight(paraCreateOrderDepotShipBean.getShipWgt());
                int i = masterOrderInfoMapper.updateByExampleSelective(record, example);

                MasterOrderInfoExtend masterOrderInfoExtend = new MasterOrderInfoExtend();
                masterOrderInfoExtend.setOutStockDate(parse);

                MasterOrderInfoExtendExample orderInfoExtendExample = new MasterOrderInfoExtendExample();
                orderInfoExtendExample.createCriteria().andMasterOrderSnEqualTo(masterOrderInfo.getMasterOrderSn());
                masterOrderInfoExtendMapper.updateByExampleSelective(masterOrderInfoExtend,orderInfoExtendExample);
            }

//            String depotCode = "DEFAULT";

//            String invoiceNo = System.currentTimeMillis() + "";
//
//            Map<String,Object> orderShipMap = new HashMap<>();
//            orderShipMap.put("orderSn",masterOrderInfo.getMasterOrderSn());
//            orderShipMap.put("depotCode",depotCode);
//            orderShipMap.put("shippingCode","LOGISTICS");
//            orderShipMap.put("invoiceNo",invoiceNo);
//            orderShipMap.put("shipDate",simpleDateFormat.parse(paraCreateOrderDepotShipBean.getStatusTime()));
//            orderShipMap.put("shipWgt",String.valueOf(paraCreateOrderDepotShipBean.getShipWgt()));
//            //下发mq，调整订单及交货单状态
//            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
//            requestSendMQBean.setSendMqTitle("strade_order_notice_ship");
//            requestSendMQBean.setSendMqMessage(JSON.toJSONString(orderShipMap));
//            mqSendService.sendCommonMQMessage(requestSendMQBean);

        } catch (Exception e) {
            log.error("OrderLogisticsServiceImpl.createOrderDepotShip()发生异常，异常信息："+e.getMessage()+" 参数："+JSON.toJSONString(paraCreateOrderDepotShipBean));
            resultApi.getSoaStateBean().setMessage("OrderLogisticsServiceImpl.createOrderDepotShip()发生异常，异常信息："+e.getMessage());
            return resultApi;
        }

        resultApi.getSoaStateBean().setCode(1);
        resultApi.getSoaStateBean().setMessage("成功");
        resultApi.setData(true);
        return resultApi;
    }

    /**
     * 接收订单配车信息
     *
     * @param paraCreateOrderDepotShipBean
     * @return
     * @author yaokan
     * @date 2021-04-05 17:36
     */
    @Override
    public APIMsgBean<Boolean> updateDriver(ParaCreateOrderDepotShipBean paraCreateOrderDepotShipBean) {
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        SOAStateBean soaStateBean = new SOAStateBean();
        soaStateBean.setCode(0);
        soaStateBean.setMessage("参数异常");
        apiMsgBean.setSoaStateBean(soaStateBean);
        if (paraCreateOrderDepotShipBean == null
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getMasterOrderSn())
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getDriverName())
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getDriverMobile())
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getPickCode())) {
            return apiMsgBean;
        }
        String masterOrderSn = paraCreateOrderDepotShipBean.getMasterOrderSn();

        MasterOrderInfo masterOrderInfo = masterOrderInfoMapper.selectByPrimaryKey(masterOrderSn);
        if (masterOrderInfo == null) {
            soaStateBean.setMessage("未查询到订单数据");
            apiMsgBean.setSoaStateBean(soaStateBean);
            return apiMsgBean;
        }
        MasterOrderInfoExtend masterOrderInfoExtend = masterOrderInfoExtendMapper.selectByPrimaryKey(masterOrderSn);
        if (masterOrderInfoExtend == null) {
            soaStateBean.setMessage("未查询到订单扩展数据");
            apiMsgBean.setSoaStateBean(soaStateBean);
            return apiMsgBean;
        }
        MasterOrderInfoExtend record = new MasterOrderInfoExtend();
        record.setMasterOrderSn(masterOrderSn);
        record.setPickCode(paraCreateOrderDepotShipBean.getPickCode());
        record.setDriverName(paraCreateOrderDepotShipBean.getDriverName());
        record.setDriverMobile(paraCreateOrderDepotShipBean.getDriverMobile());
        record.setCarNumber(paraCreateOrderDepotShipBean.getCarNumber());
        int i = masterOrderInfoExtendMapper.updateByPrimaryKeySelective(record);
        if (i > 0) {
            soaStateBean.setCode(1);
            soaStateBean.setMessage("修改成功");
            apiMsgBean.setSoaStateBean(soaStateBean);
            return apiMsgBean;
        }
        return apiMsgBean;
    }

    /**
     * 车辆进场，订单确认接口
     * @param paraCreateOrderDepotShipBean
     * @return
     * @author yaokan
     * @date 2021-04-05 17:34
     */
    @Override
    public APIMsgBean<Boolean> orderConfirm(ParaCreateOrderDepotShipBean paraCreateOrderDepotShipBean) {
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        SOAStateBean soaStateBean = new SOAStateBean();
        soaStateBean.setCode(0);
        soaStateBean.setMessage("参数异常");
        apiMsgBean.setSoaStateBean(soaStateBean);
        if (paraCreateOrderDepotShipBean == null
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getMasterOrderSn())
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getShipOrderSn())
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getEmptyCarDate())
                || StringUtils.isBlank(paraCreateOrderDepotShipBean.getOutStockSn())) {
            return apiMsgBean;
        }
        String masterOrderSn = paraCreateOrderDepotShipBean.getMasterOrderSn();

        MasterOrderInfo masterOrderInfo = masterOrderInfoMapper.selectByPrimaryKey(masterOrderSn);
        if (masterOrderInfo == null) {
            soaStateBean.setMessage("未查询到订单数据");
            apiMsgBean.setSoaStateBean(soaStateBean);
            return apiMsgBean;
        }
        MasterOrderInfoExtend masterOrderInfoExtend = masterOrderInfoExtendMapper.selectByPrimaryKey(masterOrderSn);
        if (masterOrderInfoExtend == null) {
            soaStateBean.setMessage("未查询到订单扩展数据");
            apiMsgBean.setSoaStateBean(soaStateBean);
            return apiMsgBean;
        }
        MasterOrderInfoExtend record = new MasterOrderInfoExtend();
        record.setMasterOrderSn(masterOrderSn);
        record.setShipOrderSn(paraCreateOrderDepotShipBean.getShipOrderSn());
        record.setOutStockSn(paraCreateOrderDepotShipBean.getOutStockSn());
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date emptyCarDate = formatter.parse(paraCreateOrderDepotShipBean.getEmptyCarDate());
            record.setEmptyCarDate(emptyCarDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = masterOrderInfoExtendMapper.updateByPrimaryKeySelective(record);
        if (i > 0) {
            RequestOrderQuery requestOrderQuery = new RequestOrderQuery();
            requestOrderQuery.setMasterOrderSn(masterOrderSn);
            RequestPayBackBean apiParaBean =  new RequestPayBackBean();
            apiParaBean.setMasterOrderSn(masterOrderSn);
            apiParaBean.setPayNote("车辆进场，确认订单");
            apiParaBean.setFrom(AssistantConstants.ASS_SERVICE_NAME_MSG);
            masterOrderOmsAPIService.updateDriverOrderConfirm(apiParaBean);
            soaStateBean.setCode(1);
            soaStateBean.setMessage("修改成功");
            apiMsgBean.setSoaStateBean(soaStateBean);
            return apiMsgBean;
        }
        return apiMsgBean;
    }

//    public SOAStateBean checkOrderDepotShipParam (ParaCreateOrderDepotShipBean paraCreateOrderDepotShipBean) {
//        SOAStateBean soaStateBean = new SOAStateBean();
//        if (paraCreateOrderDepotShipBean == null) {
//            soaStateBean.setMessage("参数不能为空！");
//            return soaStateBean;
//        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getOutContractSn())) {
////            soaStateBean.setMessage("合同号不能为空！");
////            return soaStateBean;
////        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getCarNumber())) {
////            soaStateBean.setMessage("车牌号不能为空！");
////            return soaStateBean;
////        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getDriverMobile())) {
////            soaStateBean.setMessage("驾驶员手机号不能为空！");
////            return soaStateBean;
////        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getPickupDate())) {
////            soaStateBean.setMessage("发车日期不能为空！");
////            return soaStateBean;
////        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getSizeMsg())) {
////            soaStateBean.setMessage("货物规格不能为空！");
////            return soaStateBean;
////        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getTradeNo())) {
////            soaStateBean.setMessage("货物材质不能为空！");
////            return soaStateBean;
////        }
////        if (StringUtils.isBlank(paraCreateOrderDepotShipBean.getUserName())) {
////            soaStateBean.setMessage("客户姓名不能为空！");
////            return soaStateBean;
////        }
////        if (paraCreateOrderDepotShipBean.getShipWgt() == null) {
////            soaStateBean.setMessage("发货重量不能为空！");
////            return soaStateBean;
////        }
//        soaStateBean.setCode(1);
//        return soaStateBean;
//    }


    @Resource
    private MasterOrderOmsAPIService masterOrderOmsAPIService;

}
